﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Extensions;
using System.Net.Mail;
using System.Text;
using System.IO;

public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //ddlShippingMethods.DataBind();
            UpdateTotals();
        }
        else
        {
            if (User.Identity.IsAuthenticated)
            {
                mvwShipping.ActiveViewIndex = 1;
                wizSubmitOrder.StepNextButtonText = wizSubmitOrder.StartNextButtonText;
            }
            else
            {
                wizSubmitOrder.StepNextButtonText = "";
                wizSubmitOrder.StepNextButtonType = ButtonType.Link;
                mvwShipping.ActiveViewIndex = 0;
            }
        }
    }

    protected void UpdateTotals()
    {
        // update the quantities
        foreach (GridViewRow row in gvwOrderItems.Rows)
        {
            int id = Convert.ToInt32(gvwOrderItems.DataKeys[row.RowIndex][0]);
            int quantity = Convert.ToInt32((row.FindControl("txtQuantity") as TextBox).Text);
            this.Profile.ShoppingCart.UpdateItemQuantity(id, quantity);
        }

        // display the subtotal and the total amounts
        lblSubtotal.Text = Profile.ShoppingCart.Total.ToMoneyString();
        lblTotal.Text = (Profile.ShoppingCart.Total + ddlShippingMethods.SelectedValue).ObjectToMoneyString();

        // if the shopping cart is empty, hide the link to proceed
        if (this.Profile.ShoppingCart.Items.Count == 0)
        {
            wizSubmitOrder.StartNextButtonText = "";
            wizSubmitOrder.StartNextButtonType = ButtonType.Link;
            panTotals.Visible = false;
        }

        gvwOrderItems.DataBind();

    }

    protected void gvwOrderItems_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        UpdateTotals();
        Response.Redirect(Request.RawUrl);
    }

    protected void btnUpdateTotals_Click(object sender, EventArgs e)
    {
        UpdateTotals();
        Response.Redirect(Request.RawUrl);
    }

    protected void wizSubmitOrder_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        // check that the user is still logged in (the cookie may have expired)
        // and if not redirect to the login page
        if (Profile.ShoppingCart.Items.Count == 0)
        {
            Response.Redirect("default.aspx");
            return;
        }
        if (this.User.Identity.IsAuthenticated)
        {
            string shippingMethod = ddlShippingMethods.SelectedItem.Text;

            // saves the order into the DB, and clear the shopping cart in the profile
            DAL.Order order = new DAL.Order
            {
                AddedDate = DateTime.Now,
                AddedBy = this.Profile.UserName,
                StatusID = 1,
                ShippingMethod = shippingMethod,
                SubTotal = Profile.ShoppingCart.Total,
                Shipping = Convert.ToDecimal(ddlShippingMethods.SelectedValue),
                ShippingFirstName = txtFirstName.Text,
                ShippingLastName = txtLastName.Text,
                ShippingStreet = txtStreet.Text,
                ShippingPostalCode = txtPostalCode.Text,
                ShippingCity = txtCity.Text,
                ShippingState = txtState.Text,
                CustomerEmail = txtEmail.Text,
                CustomerPhone = txtPhone.Text,
                CustomerFax = txtFax.Text,
                ShippedDate = null,
                ShippingCountry = ""
            };
            order.PlaceOrder(Profile.ShoppingCart);
            this.Profile.ShoppingCart.Clear();
            SaveProfileShippingDetails();
            Response.Redirect(string.Format("OrderTracking.aspx?OrderId={0}", order.OrderID));
        }
        else
            this.RequestLogin();
    }

   


    private void SaveProfileShippingDetails()
    {
         ProfileCommon profile = this.Profile;
         profile.FirstName = txtFirstName.Text;
         profile.LastName = txtLastName.Text;
         profile.Address.Street = txtStreet.Text;
         profile.Address.City = txtCity.Text;
         profile.Address.PostalCode = txtPostalCode.Text;
         profile.Address.State = txtState.Text;
         profile.Contacts.Phone = txtPhone.Text;
         profile.Contacts.Fax = txtFax.Text;
         profile.Save();
    }

    protected void wizSubmitOrder_ActiveStepChanged(object sender, EventArgs e)
    {
        if (Profile.ShoppingCart.Items.Count == 0)
        {
            Response.Redirect("default.aspx");
            return;
        }
        if (wizSubmitOrder.ActiveStepIndex == 1)
        {
            UpdateTotals();

            if (this.User.Identity.IsAuthenticated)
            {

                if (txtFirstName.Text.Trim().Length == 0)
                    txtFirstName.Text = this.Profile.FirstName;
                if (txtLastName.Text.Trim().Length == 0)
                    txtLastName.Text = this.Profile.LastName;
                if (txtEmail.Text.Trim().Length == 0)
                    txtEmail.Text = Membership.GetUser().Email;
                if (txtStreet.Text.Trim().Length == 0)
                    txtStreet.Text = this.Profile.Address.Street;
                if (txtPostalCode.Text.Trim().Length == 0)
                    txtPostalCode.Text = this.Profile.Address.PostalCode;
                if (txtCity.Text.Trim().Length == 0)
                    txtCity.Text = this.Profile.Address.City;
                if (txtState.Text.Trim().Length == 0)
                    txtState.Text = this.Profile.Address.State;
                if (txtPhone.Text.Trim().Length == 0)
                    txtPhone.Text = this.Profile.Contacts.Phone;
                if (txtFax.Text.Trim().Length == 0)
                    txtFax.Text = this.Profile.Contacts.Fax;
            }
        }
        else if (wizSubmitOrder.ActiveStepIndex == 2)
        {
            lblReviewFirstName.Text = txtFirstName.Text;
            lblReviewLastName.Text = txtLastName.Text;
            lblReviewStreet.Text = txtStreet.Text;
            lblReviewCity.Text = txtCity.Text;
            lblReviewState.Text = txtState.Text;
            lblReviewPostalCode.Text = txtPostalCode.Text;
           
            lblReviewSubtotal.Text = Profile.ShoppingCart.Total.ToMoneyString();
            lblReviewShippingMethod.Text = ddlShippingMethods.SelectedItem.Text;
            lblReviewTotal.Text = (this.Profile.ShoppingCart.Total +
               Convert.ToDecimal(ddlShippingMethods.SelectedValue)).ToMoneyString();
        }
    }
}
